﻿Imports Aricie.DNN.ComponentModel
Imports System.ComponentModel
Imports Aricie.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports Aricie.DNN.Services.Filtering
Imports Aricie.DNN.UI.WebControls.EditControls
Imports System.Xml.Serialization
Imports Aricie.DNN.UI.Attributes
Imports Aricie.DNN.UI.WebControls

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    Public Interface IUrlGroup
        Function GetRewrite(ByVal writer As UrlRewriteContext) As GroupRewriteResult
    End Interface

    <ActionButton(IconName.Cog, IconOptions.Normal)> _
    <DefaultProperty("FriendlyName")> _
    <Serializable()> _
    Public MustInherit Class UrlGroup
        Implements IUrlGroup
        Implements IEnabled

        <Browsable(False)> _
        Public Overridable ReadOnly Property FriendlyName As String
            Get
                Return Me.GetName
            End Get
        End Property

        'Inherits NamedConfig

        Public Property Enabled As Boolean = True Implements IEnabled.Enabled


        ' voir If objGroup.Visibility = UrlGroupVisibility.MasterDuplicate Then - 
        ' C:\dev\dnn6\Website\DesktopModules\Aricie.FriendlierUrlProvider\Components\Entities\Groups\FriendlierUrlRule.vb(112, 29) 
        ' pour la raison de cette dissimulation

        <ExtendedCategory("FilterSettings")> _
        Public Overridable Property EnableFilter() As Boolean = False

        <ExtendedCategory("FilterSettings")> _
        <ConditionalVisible("EnableFilter", False, True)> _
         <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
         <LabelMode(LabelMode.Top)> _
        Public Property ExpressionFilter() As New ExpressionFilterInfo(DefaultTransforms.UrlPart)



        <Browsable(False)> _
        <ExtendedCategory("TechnicalSettings")> _
        Public Property Visibility() As UrlGroupVisibility = UrlGroupVisibility.Always

        ' voir If objGroup.Visibility = UrlGroupVisibility.MasterDuplicate Then - 
        ' C:\dev\dnn6\Website\DesktopModules\Aricie.FriendlierUrlProvider\Components\Entities\Groups\FriendlierUrlRule.vb(112, 29) 
        ' pour la raison de cette dissimulation
        <Browsable(False)> _
        <ExtendedCategory("TechnicalSettings")> _
        Public Property MasterGroupName() As String = String.Empty



        <Browsable(False)> _
        <ExtendedCategory("TechnicalSettings")> _
        Public Property EnableTokenReplace() As Boolean = False

        <Browsable(False)> _
        <ExtendedCategory("TechnicalSettings")> _
        <ConditionalVisible("EnableTokenReplace", False, True)> _
        <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
        <LabelMode(LabelMode.Top)> _
        Public Property AdditionalTokenSource() As New TokenSourceInfo()


        <ExtendedCategory("TechnicalSettings")> _
        Public Overridable ReadOnly Property SupportsEntityToken() As Boolean
            Get
                Return False
            End Get
        End Property

        <ExtendedCategory("TechnicalSettings")> _
        <ConditionalVisible("SupportsEntityToken", False, False)> _
        Public Property EntityToken() As String = String.Empty

        <ExtendedCategory("TechnicalSettings")> _
        Public Property ComputeOrder() As Integer = 1




        <ExtendedCategory("ConditionalBehaviour")> _
        Public Property EnableConditionalVisibility() As Boolean

        <ExtendedCategory("ConditionalBehaviour")> _
        <ConditionalVisible("EnableConditionalVisibility", False, True)> _
        Public Property ComputeWhenHidden As Boolean

        <ExtendedCategory("ConditionalBehaviour")> _
        <ConditionalVisible("EnableConditionalVisibility", False, True)> _
        Public Property Condition As Aricie.DNN.Services.Flee.SimpleExpression(Of Boolean) = New Aricie.DNN.Services.Flee.SimpleExpression(Of Boolean)


        Public Function GetRewrite(ByVal writer As UrlRewriteContext) As GroupRewriteResult Implements IUrlGroup.GetRewrite
            Dim toReturn As GroupRewriteResult
            If Not Me.EnableConditionalVisibility Then
                toReturn = Me.ProcessRewrite(writer)
            Else
                Dim isMatch As Boolean = Me.Condition.Evaluate(writer.RewriteInput.CurrentContextOwner, Nothing)
                If isMatch OrElse ComputeWhenHidden Then
                    toReturn = Me.ProcessRewrite(writer)
                    If ComputeWhenHidden Then
                        toReturn.Clear(False)
                    End If
                Else
                    toReturn = New GroupRewriteResult
                End If
            End If
            Return toReturn
        End Function

        Protected MustOverride Function ProcessRewrite(ByVal writer As UrlRewriteContext) As GroupRewriteResult


        Protected MustOverride Function GetName() As String

        Public Overrides Function ToString() As String

            Return Me.ToStringInternal()

        End Function


        Public Overridable Function GetSeparationChar() As String
            Return Me.ExpressionFilter.DefaultCharReplacement
        End Function

        Public Overridable Function GetParamValueChar() As String
            Return Me.ExpressionFilter.DefaultCharReplacement
        End Function

        Public Function GetSeparationChar(ByVal objRewriteType As RewriteType) As String
            Select Case objRewriteType
                Case RewriteType.QueryStringValue, RewriteType.QueryStringFlag
                    Return "&"
                Case RewriteType.SubDomain, RewriteType.PageName
                    Return "."
                Case RewriteType.SubPath, RewriteType.SubPathSingle
                    Return "/"
                Case Else
                    Return Me.ExpressionFilter.DefaultCharReplacement
            End Select
        End Function

        Public Function GetParamValueChar(ByVal objRewriteType As RewriteType) As String
            Select Case objRewriteType
                Case RewriteType.QueryStringValue, RewriteType.QueryStringFlag
                    Return "="
                Case RewriteType.SubDomain, RewriteType.PageName
                    Return "."
                Case RewriteType.SubPath, RewriteType.SubPathSingle
                    Return "/"
                Case Else
                    Return Me.ExpressionFilter.DefaultCharReplacement
            End Select
        End Function






        Protected Overridable Function ToStringInternal() As String
            Return GetName()
        End Function


#Region "Methods"

        Public Overridable Function GetAdvancedTokenReplace(ByVal input As UrlRewriteInput) As AdvancedTokenReplace
            Dim toReturn As New AdvancedTokenReplace()
            toReturn.SetObjectReplace(input.Context, "Context")
            toReturn.SetObjectReplace(input, "Input")
            Me._AdditionalTokenSource.SetTokens(toReturn)
            Return toReturn
        End Function


#End Region


    End Class
End Namespace
